var SysUserStateChangeWinDatagrid = new HgUi.Datagrid("SysUserStateChangeWin_datagrid");
SysUserStateChangeWinDatagrid.toolbar = $("#SysUserStateChangeWin_toolbar");
SysUserStateChangeWinDatagrid.options.isShowContextMenu = true;
SysUserStateChangeWinDatagrid.options.tipCells 
	= ["lockType","ip","originState","currentState","recordType","descr","crtTime"];
SysUserStateChangeWinDatagrid.options.onSelect = function(index,row) {
    if (row && row.dictGroupId){
        SysUserStateChangeWinDatagrid.selectedRow = row;
    }
};
SysUserStateChangeWinDatagrid.options.onLoadSuccess = function(data) {
    SysUserStateChangeWinDatagrid.grid.datagrid("selectRow",0);
};
SysUserStateChangeWinDatagrid.init();

SysUserStateChangeWinDatagrid.grid.datagrid("enableFilter",[
	{
		field: 'type',
		type: 'combobox',
		options: {
			panelHeight:'auto',
			editable:false,
			prompt: '---请选择---',
			data:HgUtil.getDictComboboxData('sysUserStateChangeType'),
			onChange: function(value) {
				if (value == ''){
					SysUserStateChangeWinDatagrid.grid.datagrid('removeFilterRule', 'type');
				}
				else{
					SysUserStateChangeWinDatagrid.grid.datagrid('addFilterRule', {
						field: 'type',
						op: 'equal',
						value: value
					});
				}
				SysUserStateChangeWinDatagrid.grid.datagrid('doFilter');
			}
		}
	},                                                        
	{
		field: 'originState',
		type: 'combobox',
		options: {
			panelHeight:'auto',
			editable:false,
			prompt: '---请选择---',
			data:HgUtil.getDictComboboxData('userStateType'),
			onChange: function(value) {
				if (value == ''){
					SysUserStateChangeWinDatagrid.grid.datagrid('removeFilterRule', 'originState');
				}
				else{
					SysUserStateChangeWinDatagrid.grid.datagrid('addFilterRule', {
						field: 'originState',
						op: 'equal',
						value: value
					});
				}
				SysUserStateChangeWinDatagrid.grid.datagrid('doFilter');
			}
		}
	},
	{
		field: 'currentState',
		type: 'combobox',
		options: {
			panelHeight:'auto',
			editable:false,
			prompt: '---请选择---',
			data:HgUtil.getDictComboboxData('userStateType'),
			onChange: function(value) {
				if (value == ''){
					SysUserStateChangeWinDatagrid.grid.datagrid('removeFilterRule', 'currentState');
				}
				else{
					SysUserStateChangeWinDatagrid.grid.datagrid('addFilterRule', {
						field: 'currentState',
						op: 'equal',
						value: value
					});
				}
				SysUserStateChangeWinDatagrid.grid.datagrid('doFilter');
			}
		}
	},
	{
		field: 'crtType',
		type: 'combobox',
		options: {
			panelHeight:'auto',
			editable:false,
			prompt: '---请选择---',
			data:HgUtil.getDictComboboxData('crtTypeUserStateChange'),
			onChange: function(value) {
				if (value == ''){
					SysUserStateChangeWinDatagrid.grid.datagrid('removeFilterRule', 'crtType');
				}
				else{
					SysUserStateChangeWinDatagrid.grid.datagrid('addFilterRule', {
						field: 'crtType',
						op: 'equal',
						value: value
					});
				}
				SysUserStateChangeWinDatagrid.grid.datagrid('doFilter');
			}
		}
	}
]);
SysUserStateChangeWinDatagrid.bindDefaultToolbarEvent();


//--------------------------保存锁定数据------------------------------------------------------------------
SysUserStateChangeWinDatagrid.toolbar.find("[tag=save]").click(function() {
    if (SysUserStateChangeWinDatagrid.isEditing == true) {
        if (SysUserStateChangeWinDatagrid.grid.datagrid("validateRow", SysUserStateChangeWinDatagrid.editingIndex)) {
            var data = SysUserStateChangeWinDatagrid.makeSubmitData();
            if(!SysUserStateChangeWinDatagrid.checkData(data)){
        		return;
        	}
            var editRow = SysUserStateChangeWinDatagrid.grid.datagrid("getRows")[SysUserStateChangeWinDatagrid.editingIndex];
            var submitUrl = "/sys/sysUserStateChange/lock";
            if (editRow.id) {
                submitUrl = "/sys/sysUserStateChange/upd";
                data.id = editRow.id;
            }else{
            	data.accountId = accountId;
            }
            
            $.messager.confirm("确认锁定", "保存后无法修改,确认将该IP锁定?", function(r){
                if (r) {
                	HgUtil.post(submitUrl, data, function(data) {
                        if (!data.success) {
                            SysUserStateChangeWinDatagrid.showMsgbox("error", data.data, 330);
                        } else {
                            SysUserStateChangeWinDatagrid.grid.datagrid('reload');
                        }
                    });
                }
            });

        } else {
            SysUserStateChangeWinDatagrid.showMsgbox("alert", "有一条数据未校验通过!", 180);
        }
    }
});

//-------------------------解锁-----------------------------------------------------------------------
SysUserStateChangeWinDatagrid.toolbar.find("[tag=unlock]").click(function(){
    var row = SysUserStateChangeWinDatagrid.grid.datagrid("getSelected");
    if (!row) {
        HgUi.alert("请选择一条数据");
        return;
    }
    if (row.currentState == 0) {
    	HgUi.alert("当前状态已为正常");
    	return;
    }
    $.messager.confirm("确认解锁", "确认将选中的数据解锁?", function(r){
        if (r) {
            HgUtil.post("/sys/sysUserStateChange/unlock", {
			    accountId: row.accountId,
			    ip: row.ip,
			    type: row.type
			}, function() {
				HgUi.ok("修改成功!", function() {
					SysUserStateChangeWinDatagrid.grid.datagrid("reload");
				});
			});
        }
    });
});

//-------------------------删除-----------------------------------------------------------------------
SysUserStateChangeWinDatagrid.toolbar.find("[tag=del]").click(function(){
    var row = SysUserStateChangeWinDatagrid.grid.datagrid("getSelected");
    if (!row) {
        $.messager.alert("提示","请选择一条数据","warning");
        return;
    }
    $.messager.confirm("确认删除", "确认将选中的数据删除?", function(r){
        if (r) {
            HgUtil.getJson("/sys/SysUserStateChange/del",{id:row.id}, function(data){
                if (!data.success) {
                    $.messager.alert("提示","删除失败,"+data.data,"warning");
                } else {
                    HgUi.ok("删除成功!",function(){
                        SysUserStateChangeWinDatagrid.grid.datagrid("reload");
                    });
                }
            });
        }
    });
});

// 组织提交数据
SysUserStateChangeWinDatagrid.makeSubmitData = function() {
    var ip = SysUserStateChangeWinDatagrid.grid.datagrid('getEditor', {
        index : SysUserStateChangeWinDatagrid.editingIndex,
        field : "ip"
    });
    var descr = SysUserStateChangeWinDatagrid.grid.datagrid('getEditor', {
        index : SysUserStateChangeWinDatagrid.editingIndex,
        field : "descr"
    });

    var data = {
        ip : $(ip.target).textbox("getValue"),
        descr : $(descr.target).textbox("getValue")
    };
    return data;
};

SysUserStateChangeWinDatagrid.checkData = function(data){
	
	//ip格式
	var ipRegx = "(?:(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)\\.){3}(?:(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d))";
	var ip = data.ip;
	if(!ip.match(ipRegx)){
		HgUi.alert("ip不符合格式!");
		return false;
	}
	
	return true;
	
}

// 清空查询---------------------------------------------------------------------------------------------------
SysUserStateChangeWinDatagrid.toolbar.find("[tag=clear]").click(function() {
	SysUserStateChangeWinDatagrid.grid.datagrid("removeFilterRule");
    SysUserStateChangeWinDatagrid.grid.datagrid("load", {});
});
